package com.wanhe.base.util;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID;

/**
 * Created by Administrator on 2016/11/28.
 */
public class EndianUtil {

    public static byte[] littleEndian(byte[] bytes) {
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        return bb.array();
    }

    public static byte[] bigEndian(byte[] bytes) {
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        bb.order(ByteOrder.BIG_ENDIAN);
        return bb.array();
    }

    public static int byte2Int(byte[] bytes) {
        int i = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
        return i;
    }
    public static short byte2Short(byte[] bytes) {
        short i = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
        return i;
    }
    public static long byte2Long(byte[] bytes) {
        long i = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getLong();
        return i;
    }

    public static byte[] uuidToByte(String uuid){
        UUID uuid2 = UUID.fromString(uuid);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
//        byte2Long(littleEndian(NumberBytes.longToBytes(uuid2.getMostSignificantBits())))

        try {
            dos.writeLong(byte2Long(NumberBytes.longToBytes2(uuid2.getMostSignificantBits())));
            dos.writeLong(uuid2.getLeastSignificantBits());
            dos.flush(); // May not be necessary
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
}
